package com.shrct.businessprocess.organization.impl;

import com.google.common.collect.Iterables;
import com.shrct.businessprocess.organization.RCEmployee;
import com.shrct.businessprocess.organization.RCEmployeeUser;
import com.shrct.businessprocess.organization.RCRoleAssignment;
import com.shrct.businessprocess.organization.RCSecurityService;
import edu.fudan.langlab.domain.security.AppFunction;
import edu.fudan.langlab.domain.security.AppFunctionService;
import edu.fudan.langlab.domain.security.RoleFunctionAssignment;
import edu.fudan.mylang.pf.BaseService;
import edu.fudan.mylang.pf.IGenericQuery;
import java.util.Collection;
import java.util.Set;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("com.shrct.businessprocess.organization.RCSecurityService")
public class RCSecurityServiceImpl extends BaseService implements RCSecurityService {
  @Autowired
  private AppFunctionService functionService;
  
  public RCEmployeeUser employeeUserLogin(final String userid, final String passwd) {
    IGenericQuery<RCEmployeeUser> query=getObjectFactory().createGenericQuery(RCEmployeeUser.class,"select u from com.shrct.businessprocess.organization.impl.RCEmployeeUserImpl as u   where (((u.userid) = (:userid)) and ((u.passwd) = (:passwd))) and  u.active = 1      ").setParameter("userid",userid).setParameter("passwd",passwd);
    RCEmployeeUser _uniqueResult = query.uniqueResult();
    return _uniqueResult;
  }
  
  public Collection<AppFunction> loadAllFunctions(final String userid) {
    Set<AppFunction> _xblockexpression = null;
    {
      IGenericQuery<RCEmployeeUser> query=getObjectFactory().createGenericQuery(RCEmployeeUser.class,"select u from com.shrct.businessprocess.organization.impl.RCEmployeeUserImpl as u   where ((u.userid) = (:userid)) and  u.active = 1      ").setParameter("userid",userid);
      final RCEmployeeUser user = query.uniqueResult();
      final RCEmployee emp = user.getEmployee();
      IGenericQuery<RCRoleAssignment> query_1=getObjectFactory().createGenericQuery(RCRoleAssignment.class,"select r from com.shrct.businessprocess.organization.impl.RCRoleAssignmentImpl as r   where ((r.employee) = (:emp)) and  r.active = 1      ").setParameter("emp",emp);
      Collection<RCRoleAssignment> _list = query_1.list();
      final Function1<RCRoleAssignment,Collection<RoleFunctionAssignment>> _function = new Function1<RCRoleAssignment,Collection<RoleFunctionAssignment>>() {
          public Collection<RoleFunctionAssignment> apply(final RCRoleAssignment ra) {
            String _roleType = ra.getRoleType();
            Collection<RoleFunctionAssignment> _loadAllFunctionAssignments4Role = RCSecurityServiceImpl.this.functionService.loadAllFunctionAssignments4Role(_roleType);
            return _loadAllFunctionAssignments4Role;
          }
        };
      Iterable<Collection<RoleFunctionAssignment>> _map = IterableExtensions.<RCRoleAssignment, Collection<RoleFunctionAssignment>>map(_list, _function);
      Iterable<RoleFunctionAssignment> _flatten = Iterables.<RoleFunctionAssignment>concat(_map);
      final Function1<RoleFunctionAssignment,AppFunction> _function_1 = new Function1<RoleFunctionAssignment,AppFunction>() {
          public AppFunction apply(final RoleFunctionAssignment it) {
            AppFunction _function = it.getFunction();
            return _function;
          }
        };
      Iterable<AppFunction> _map_1 = IterableExtensions.<RoleFunctionAssignment, AppFunction>map(_flatten, _function_1);
      Set<AppFunction> _set = IterableExtensions.<AppFunction>toSet(_map_1);
      _xblockexpression = (_set);
    }
    return _xblockexpression;
  }
}
